import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from 'nprogress';
import findLast from 'lodash/findLast'
import { notification } from "ant-design-vue";
import 'nprogress/nprogress.css';
import NotFound from '../views/User/404'
import Forbidden from "../views/User/403";
import {check, isLogin} from '../utils/auth'

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    {
      path: "/user",
      hideInMenu: true,
      component: () => 
        import(/* webpackChunkName: "user" */ "../layouts/UserLayout.vue"),
      children: [
        {
          path: "/user",
          redirect: "/user/login",
        },
        {
          path: "/user/login",
          name: "login",
          component: () => 
            import(/* webpackChunkName: "user" */ "../views/User/Login.vue")
        },
        {
          path: "/user/register",
          name: "register",
          component: () =>
            import(/* webpackChunkName: "user" */ "../views/User/Register.vue")
        },
      ]
    },
    {
      path:"/",
      meta: {authority: ["user", "admin"]},
      component: () => 
        import(/* webpackChunkName: "user" */ "../layouts/BasicLayout.vue"),
      children: [
        //dashboard
        {
          path: "/",
          redirect: "/dashboard/analysis"
        },
        {
          path: "/dashboard",
          name: "dashboard",
          meta: {icon: 'dashboard', title: '仪表盘'},
          component: { render: h => h("router-view")},
          children: [
            {
              path: "/dashboard/analysis",
              name: "analysis",
              meta: {title: '分析页'},
              component: () => 
                import(/* webpackChunkName: "user" */ "../views/Dashboard/Analysis.vue")
            }
          ]
        },

        //form
        {
          path: "/form",
          name: 'form',
          component: { render: h => h("router-view")},
          meta: {icon: 'form', title: '表单', authority: ["admin"]},
          children: [
            {
              path: "/form/basic-form",
              name: "basicform",
              meta: {title: '基础表单'},
              component: () => 
                import(/* webpackChunkName: "form" */ "../views/Forms/BasicForm.vue")
            },
            {
              path: "/form/step-form",
              name: "stepform",
              meta: {title: '分布表单'},
              hideChildrenInMenu: true,
              component: () =>  
                import(/* webpackChunkName: "user" */ "../views/Forms/Stepform/index.vue"),
              children: [
                {
                  path: "/form/step-form",
                  redirect: "/form/step-form/info"
                },
                {
                  path: "/form/step-form/info",
                  name: "info",
                  component: () => 
                    import(/* webpackChunkName: "user" */ "../views/Forms/Stepform/Step1.vue"),
                },
                {
                  path: "/form/step-form/confirm",
                  name: "confirm",
                  component: () => 
                    import(/* webpackChunkName: "user" */ "../views/Forms/Stepform/Step2.vue"),
                },
                {
                  path: "/form/step-form/result",
                  name: "result",
                  component: () => 
                    import(/* webpackChunkName: "user" */ "../views/Forms/Stepform/Step3.vue"),
                },
              ]
            }
          ],
        }
      ]
    },
    {
      path: "/403",
      name: "403",
      hideInMenu:true,
      component: Forbidden
    },
    {
      path: "*",
      name: "404",
      hideInMenu:true,
      component: NotFound
    }
  ]
});

router.beforeEach((to, from, next) => {
  
  if(to.path !== from.path){
    NProgress.start();
  }
  const record = findLast(to.matched, record => record.meta.authority)
  if(record && !check(record.meta.authority)){
    if(!isLogin() && to.path !== '/user/login') {
      next({
        path: '/user/login'
      })
    }else if(to.path !== "/403"){
      notification.error({
        message: "403",
        description: "你没有权限访问，请联系管理员咨询。"
      });
      next({
        path: '/403'
      })
    }
    NProgress.done()
  }
  next();
})
router.afterEach(() => {
  NProgress.done()
})

export default router;
